package com.lxk.service.impl;

import com.lxk.dao.AsMyMapper;
import com.lxk.dao.CouponMapper;
import com.lxk.dao.TemMapper;
import com.lxk.pojo.Coupon;
import com.lxk.pojo.CouponExample;
import com.lxk.pojo.Tem;
import com.lxk.service.CouponService;
import com.lxk.vo.CouponInfoModel;
import com.lxk.vo.CouponShowModel;
import com.lxk.vo.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/4/8 16:24
 * @description
 */
@Service
@Transactional
public class CouponServiceImpl implements CouponService {

    @Autowired
    private AsMyMapper mm;
    @Autowired
    private TemMapper tm;
    @Autowired
    private CouponMapper cm;

    public Integer getTemTotalPages(Integer num) {
        Integer count = mm.getTemTotalNum();
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<Tem> selTemPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<Tem> list = mm.selTemPage(start, num, sort, order);
        PageModel<Tem> model = new PageModel<Tem>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Integer getCouponTotalPages(Integer num) {
        CouponExample example = new CouponExample();
        Integer count = (int)cm.countByExample(example);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<CouponShowModel> selCouponPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<CouponShowModel> list = mm.selCouponPage(start, num, sort, order);
        PageModel<CouponShowModel> model = new PageModel<CouponShowModel>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public void addNewTem(Tem tem) {
        tm.insertSelective(tem);
    }

    public void updTem(Tem tem) {
        tm.updateByPrimaryKeySelective(tem);
    }

    public void deleteTem(Integer temId) {
        tm.deleteByPrimaryKey(temId);
    }

    public Integer generateCoupon(Integer temId) {
        //获取优惠券模板信息
        Tem tem = tm.selectByPrimaryKey(temId);
        Integer temTimespan = tem.getTemTimespan();
        Date nowTime = new Date();
        //将初始时间设为当天的0点0分0秒
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.add(Calendar.DATE, temTimespan);
        cal.add(Calendar.SECOND, -1);
        Date endTime = cal.getTime();
        List<Coupon> list = new ArrayList<>();
        //获取用户id列表
        List<Integer> wxUserIdList = mm.getWxUserIdList();
        for (Integer wxUserId : wxUserIdList) {
            //设置优惠券信息
            Coupon coupon = new Coupon();
            coupon.setCouponTime(nowTime);
            coupon.setEndTime(endTime);
            coupon.setTemId(temId);
            coupon.setWxUserId(wxUserId);
            list.add(coupon);
        }
        mm.insertBatchCoupon(list);
        return wxUserIdList.size();
    }

    public Coupon selCouponById(Integer couponId) {
        Coupon coupon = cm.selectByPrimaryKey(couponId);
        return coupon;
    }

    public Tem selTemById(Integer temId) {
        Tem tem = tm.selectByPrimaryKey(temId);
        return tem;
    }

    public List<CouponInfoModel> getAllAvailCoupon(Integer wxUserId) {
        List<CouponInfoModel> returnList = new ArrayList<CouponInfoModel>();
        List<CouponInfoModel> list = mm.getUnusedCouponInfo(wxUserId);
        for (CouponInfoModel cim : list) {
            Integer isExpired = getIsExpired(cim.getEndTime());
            cim.setIsExpired(isExpired);
            if(isExpired==0) {	//是否过期
                returnList.add(cim);
            }
        }
        return returnList;
    }

    //判断是否过期
    private Integer getIsExpired(Date endTime) {
        Integer isExpired = 0;
        Date nowTime = new Date();
        if(nowTime.after(endTime)) {
            isExpired = 1;
        }
        return isExpired;
    }

}
